﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using Agresso.Interface.Layer.Domain;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;

namespace Agresso.Interface.Layer.Service
{
    public class ConfigurationSettingService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public ConfigurationSettingService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public ConfigurationSetting GetConfigurationSetting(string settingName)
        {
            ConfigurationSetting result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ConfigurationSettingSQL.GetBySettingName(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@setting_name", System.Data.SqlDbType.NVarChar).Value = settingName;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new ConfigurationSetting(rd);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }
        public void UpdateConfiguration(ConfigurationSetting cs)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ConfigurationSettingSQL.Update(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@setting_name", System.Data.SqlDbType.NVarChar).Value = cs.setting_name;
                cmd.Parameters.Add("@val_1", System.Data.SqlDbType.NVarChar).Value = cs.val_1;
                cmd.Parameters.Add("@val_2", System.Data.SqlDbType.NVarChar).Value = cs.val_2;
                cmd.Parameters.Add("@val_3", System.Data.SqlDbType.NVarChar).Value = cs.val_3;
                cmd.Parameters.Add("@val_4", System.Data.SqlDbType.NVarChar).Value = cs.val_4;
                cmd.Parameters.Add("@val_5", System.Data.SqlDbType.NVarChar).Value = cs.val_5;
                cmd.Parameters.Add("@val_6", System.Data.SqlDbType.NVarChar).Value = cs.val_6;
                cmd.Parameters.Add("@val_7", System.Data.SqlDbType.NVarChar).Value = cs.val_7;
                cmd.Parameters.Add("@val_8", System.Data.SqlDbType.NVarChar).Value = cs.val_8;
                cmd.Parameters.Add("@val_9", System.Data.SqlDbType.NVarChar).Value = cs.val_9;
                cmd.Parameters.Add("@val_10", System.Data.SqlDbType.NVarChar).Value = cs.val_10;
                cmd.Parameters.Add("@val_11", System.Data.SqlDbType.NVarChar).Value = cs.val_11;
                cmd.Parameters.Add("@val_12", System.Data.SqlDbType.NVarChar).Value = cs.val_12;
                cmd.Parameters.Add("@val_13", System.Data.SqlDbType.NVarChar).Value = cs.val_13;
                cmd.Parameters.Add("@val_14", System.Data.SqlDbType.NVarChar).Value = cs.val_14;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }

        }
        public void InsertConfiguration(ConfigurationSetting cs)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(ConfigurationSettingSQL.Insert(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@setting_name", System.Data.SqlDbType.NVarChar).Value = cs.setting_name;
                cmd.Parameters.Add("@val_1", System.Data.SqlDbType.NVarChar).Value = cs.val_1;
                cmd.Parameters.Add("@val_2", System.Data.SqlDbType.NVarChar).Value = cs.val_2;
                cmd.Parameters.Add("@val_3", System.Data.SqlDbType.NVarChar).Value = cs.val_3;
                cmd.Parameters.Add("@val_4", System.Data.SqlDbType.NVarChar).Value = cs.val_4;
                cmd.Parameters.Add("@val_5", System.Data.SqlDbType.NVarChar).Value = cs.val_5;
                cmd.Parameters.Add("@val_6", System.Data.SqlDbType.NVarChar).Value = cs.val_6;
                cmd.Parameters.Add("@val_7", System.Data.SqlDbType.NVarChar).Value = cs.val_7;
                cmd.Parameters.Add("@val_8", System.Data.SqlDbType.NVarChar).Value = cs.val_8;
                cmd.Parameters.Add("@val_9", System.Data.SqlDbType.NVarChar).Value = cs.val_9;
                cmd.Parameters.Add("@val_10", System.Data.SqlDbType.NVarChar).Value = cs.val_10;
                cmd.Parameters.Add("@val_11", System.Data.SqlDbType.NVarChar).Value = cs.val_11;
                cmd.Parameters.Add("@val_12", System.Data.SqlDbType.NVarChar).Value = cs.val_12;
                cmd.Parameters.Add("@val_13", System.Data.SqlDbType.NVarChar).Value = cs.val_13;
                cmd.Parameters.Add("@val_14", System.Data.SqlDbType.NVarChar).Value = cs.val_14;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }

        }
    }
}
